﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Client
{
    /// <summary>
    /// 挂载在界面预制上的脚本
    /// </summary>
    public class UIVisual : MonoBehaviour
    {
        private static Vector3 far = new Vector3(0, 0, -9999);

        /// <summary>
        /// 锚点枚举
        /// </summary>
        public enum EAnchor
        {
            TopLeft,
            Top,
            TopRight,
            Left,
            Center,
            Right,
            BottomLeft,
            Bottom,
            BottomRight,
        }

        public Canvas Canvas;

        public GraphicRaycaster GraphicRaycaster;

        /// <summary>
        /// 锚点
        /// </summary>
        public List<Transform> Anchors;

        /// <summary>
        /// 节点路径
        /// </summary>
        public List<string> Paths;

        /// <summary>
        /// 节点
        /// </summary>
        public List<GameObject> GameObjects;

        /// <summary>
        /// 设置父节点
        /// </summary>
        /// <param name="parent"></param>
        public void SetParent(Transform parent)
        {
            transform.SetParent(parent, false);
            transform.localPosition = far;
        }

        /// <summary>
        /// 消失表现
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="directly"></param>
        public void DoDisappear(Action callback, bool directly)
        {
            if (directly)
            {
                transform.localPosition = far;
                Global.UIManager.SubRaycastCanvas(Canvas);
                if (callback != null)
                {
                    callback();
                }
            }
            else
            {
            }
        }

        /// <summary>
        /// 出现表现
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="directly"></param>
        public void DoAppear(Action callback, bool directly)
        {
            if (directly)
            {
                transform.localPosition = Vector3.zero;
                Global.UIManager.AddRaycastCanvas(Canvas);
                if (callback != null)
                {
                    callback();
                }
            }
            else
            {
            }
        }

        public GameObject GetGameObjectAtChild(string path)
        {
            var index = Paths.IndexOf(path);
            if (index != -1)
            {
                return GameObjects[index];
            }
            return null;
        }

        public Text GetTextAtChild(string path)
        {
            var index = Paths.IndexOf(path);
            if (index != -1)
            {
                var go = GameObjects[index];
                return go.GetComponent<Text>();
            }
            return null;
        }

        public Button GetButtonAtChild(string path)
        {
            var index = Paths.IndexOf(path);
            if (index != -1)
            {
                var go = GameObjects[index];
                return go.GetComponent<Button>();
            }
            return null;
        }

        public InputField GetInputFieldAtChild(string path)
        {
            var index = Paths.IndexOf(path);
            if (index != -1)
            {
                var go = GameObjects[index];
                return go.GetComponent<InputField>();
            }
            return null;
        }
    }
}
